//
//  ActViewController.m
//  fupengdouban
//
//  Created by lanou3g on 15/10/25.
//  Copyright © 2015年 fuoeng. All rights reserved.
//

#import "ActViewController.h"
#import "ActView.h"
#import "UIImageView+WebCache.h"
#import <AVUser.h>
#import "LoginViewController.h"
#import "CollectionDataHandle.h"

@interface ActViewController ()

@property(nonatomic, strong)ActView * av;


@end

@implementation ActViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.av.titleLabel.text = self.message.title;
    [self.av.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:25]];
    
    NSRange x = {5,11};
    NSString * s1 = [self.message.begin_time substringWithRange:x];
    NSString * s2 = [self.message.end_time substringWithRange:x];
    
    self.av.beginEndTimeLabel.text = [NSString stringWithFormat:@"%@ -- %@",s1,s2];
    self.av.nameLabel.text = self.message.name;
    self.av.categoryNameLabel.text = self.message.category_name;
    self.av.addressLabel.text = self.message.address;
    
 
    if (!self.message.activityImg) {
        [self.av.image sd_setImageWithURL:[NSURL URLWithString:self.message.image]];
        
        
    } else {
        // 保存照片
        self.av.image.image = self.message.activityImg;
        
    }
    
    
    
    
    
    self.av.ActLabel.text = @"活动介绍";
    [self.av.ActLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    self.av.ActTextLabel.text = self.message.content;
    
    self.navigationItem.title = self.message.title;
    
    CGFloat i = [self p_heightWithString:self.message.content];
    self.av.ActTextLabel.frame = CGRectMake(CGRectGetMinX(self.av.ActLabel.frame), CGRectGetMaxY(self.av.ActLabel.frame)+10, CGRectGetWidth(self.av.titleLabel.frame)-20, i);
    
    self.av.scrllView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.av.ActTextLabel.frame)+60);
    
    

    
}




-(void)loadView
{
    self.av = [[ActView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.av;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    // 左Button
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"第一页-跳转后-返回图1"] style:(UIBarButtonItemStylePlain) target:self action:@selector(liftAction:)];
    // 右Button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"第一页-跳转后-收藏图1"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction:)];
    

}


-(void)liftAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


// LQH-收藏
-(void)rightAction:(UIButton *)sender
{
    // 判断是否有账户登录
    if (![AVUser currentUser]) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        [self presentViewController:loginVC animated:YES completion:^{
            
        }];
        
    }
    
    // 添加收藏
    
    // 打开数据库
    [[CollectionDataHandle sharedCollection] openDB];
    
    [[CollectionDataHandle sharedCollection] createTableAct];
    
    if ([[CollectionDataHandle sharedCollection] isExistAct:self.message.a_id] == YES) {
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"已存在收藏夹" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
           
            sleep(0.5);
            [alert dismissViewControllerAnimated:true completion:^{
                
            }];
        }];

    } else {
    
        [[CollectionDataHandle sharedCollection] insertAct:self.message];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"收藏成功"preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
            sleep(0.5);
            [alert dismissViewControllerAnimated:true completion:^{
                
            }];
        }];

        
    }
    

    
    // 关闭数据库
    [[CollectionDataHandle sharedCollection] closeDB];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






-(CGFloat)p_heightWithString:(NSString *)aString
{
    CGRect r = [aString boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.av.ActTextLabel.frame), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f]} context:nil];
    return r.size.height + 50;
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
